package mapper

import (
	"context"
	"database/sql"
	"fmt"
	"go-zero-project/services/product/rpc/internal/mapper/entity"
)

type ProductMapper struct {
}

func NewProductMapper() *ProductMapper {
	return &ProductMapper{}
}

func (m *ProductMapper) TxAdjustStock(ctx context.Context, tx *sql.Tx, id int64, delta int) (sql.Result, error) {
	productEntity := entity.ProductEntity{}
	query := fmt.Sprintf("update %s set stock=stock+? where stock  > -? and id=?", productEntity.TableName())
	return tx.ExecContext(ctx, query, delta, delta, id)
}
